#include <iostream>
#include <sstream>
#include <string>

#include "coapconstants.h"
#include "options.h"

using namespace std;

///////////////////////////////////////////////////////
// Public methods
///////////////////////////////////////////////////////

// Constructor
Options::Options()
{} 

Options::~Options()  
{} 

//void Options::parseOptions(Options& ret, Frame& options, Uint16 optionsCount)
void Options::parseOptions(const Frame& options, const Uint16 optionsCount)
{
   //std::cout << "Number of options: " << optionsCount << std::endl;

   Uint16 jj = 0;
   Uint8 optionDeltaPrev = 0;   
   Uint8 optionDeltaCurr = 0;
   options_map.clear();

   for (int ii = 0; ii < optionsCount; ii++) {
      Uint16 optionDeltaCurr = options.read(jj) & 0x0f;
      Uint16 optionNumber = optionDeltaCurr + optionDeltaPrev;
      Uint16 optionLength = (options.read(jj++) & 0xf0) >> 4;
      if (optionLength == 0xf)
		optionLength = static_cast<Uint16>(options.read(jj++)) + 0xf;
      
      std::string val = "";
      Uint16 opCounter = 1;
      for (Uint16 kk = 0; kk < optionLength; kk++) 
      	val += options.read(jj++);

      // Debug
      // std::cout << "Option " << ii << ": " 	<< val	<< std::endl;
      
      options_map.insert(std::make_pair(optionNumber, val));
      optionDeltaPrev = optionDeltaCurr;
   }
}

bool Options::isCriticalOK(void)
{
   // TODO: check all critical options here
   return true;
}

std::string Options::getOption(Uint32 optionNum)
{
   return options_map[optionNum];
}

